import { fileURLToPath, URL } from "node:url"
import { loadEnv, type UserConfig } from "vite"
import { vitePlugins, postcssPlugins } from "./plugins"
import type { ImportMetaEnv } from "./types/env"

export default (mode: string): UserConfig => {
  const env = loadEnv(mode, fileURLToPath(new URL("./", import.meta.url))) as unknown as ImportMetaEnv

  return {
    root: fileURLToPath(new URL("./", import.meta.url)),
    plugins: vitePlugins,
    resolve: {
      extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".scss", ".css", ".vue"],
      alias: {
        "@": fileURLToPath(new URL("./src", import.meta.url)),
        "@com": fileURLToPath(new URL("./src/components", import.meta.url))
      }
    },
    css: {
      modules: {
        localsConvention: "camelCaseOnly",
        hashPrefix: "BoSi"
      },
      preprocessorOptions: {
        // 后面有关的scss配置在这里配置
        scss: {}
      },
      devSourcemap: "true".includes(env.VITE_SOURCEMAP),
      postcss: {
        plugins: postcssPlugins
      }
    },
    json: {
      stringify: true
    },
    clearScreen: "true".includes(env.VITE_CLEARSCREEN)
  }
}
